package factory;

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;

import dao.ClienteDao;
import dao.CondicaoTesteDao;
import dao.ConfirmacaoPedidoDao;
import dao.CriterioAceitacaoDao;
import dao.FluidoDao;
import dao.PinturaDao;

public class ConnectionFactory {
	
	public static final int MYSQL = 0;
	public static final int MYSQLNDB = 1;
	
	public static String driverMySQL = "com.mysql.jdbc.Driver";
	public static String urlSQL = "jdbc:mysql://192.168.1.101:3306/ndb_teste_bomba_2";
	public static String urlSQLNDB = "jdbc:mysql://localhost:3306/ndb_teste_bomba_2";
	public static Connection conexao = null;
	public static int tipoGravacao;
	public static FluidoDao fluidoDao;
	public static ClienteDao clienteDao;
	public static CriterioAceitacaoDao criterioAceitacaoDao;
	public static CondicaoTesteDao condicaoTesteDao;
	public static PinturaDao pinturaDao;
	public static ConfirmacaoPedidoDao confirmacaoPedidoDao;
	
	public ConnectionFactory(int tipo) {		
		tipoGravacao = tipo;
		if (tipoGravacao == MYSQL) {
			try {
				Class.forName(driverMySQL);
				conexao = DriverManager.getConnection(urlSQL, "root", "0511984");				
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		} else {
			if (tipoGravacao == MYSQLNDB) {
				try {
					Class.forName(driverMySQL);
					conexao = DriverManager.getConnection(urlSQLNDB, "root", "1234");				
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				} catch (SQLException e) {
					e.printStackTrace();
				}
			}
		}
	}	
	
	public Connection getConnection() throws ClassNotFoundException {		
		return conexao;
	}
	
	public FluidoDao getFluidoDao() {
		if (fluidoDao == null) {
			try {
				fluidoDao = new FluidoDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return fluidoDao;
	}
	
	public ClienteDao getClienteDao() {
		if (clienteDao == null) {
			try {
				clienteDao = new ClienteDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return clienteDao;
	}
	
	public CriterioAceitacaoDao getCriterioAceitacaoDao() {
		if (criterioAceitacaoDao == null) {
			try {
				criterioAceitacaoDao = new CriterioAceitacaoDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return criterioAceitacaoDao;
	}
	
	public CondicaoTesteDao getCondicaoTesteDao() {
		if (condicaoTesteDao == null) {
			try {
				condicaoTesteDao = new CondicaoTesteDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return condicaoTesteDao;
	}
	
	public PinturaDao getPinturaDao() {
		if (pinturaDao == null) {
			try {
				pinturaDao = new PinturaDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return pinturaDao;
	}
	
	public ConfirmacaoPedidoDao getConfirmacaoPedidoDao() {
		if (confirmacaoPedidoDao == null) {
			try {
				confirmacaoPedidoDao = new ConfirmacaoPedidoDao(conexao);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return confirmacaoPedidoDao;
	}
}
